﻿Imports lbGlobalcom
Public Class fRecibosPlanes
    Dim iTipoDoc As cTipoDocumento
    Dim daCli As lbGlobalcom.dsTerceroTableAdapters.TER_CLIENTESTableAdapter
    Dim daRecibi As lbGlobalcom.dsCarteraTableAdapters.CAR_RECIBOSCAJATableAdapter
    Dim daRecibiDtalle As lbGlobalcom.dsCarteraTableAdapters.CAR_RECIBOCAJADETALLEPLANTableAdapter
    Dim daMovi As lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
    Dim daTipodoc As lbGlobalcom.dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
    Dim daConcepto As lbGlobalcom.dsCarteraTableAdapters.CAR_CONCEPTOPAGOTableAdapter
    'Dim cCart As lbGlobalcom.cCartera
    Dim lEliminaDoc As Boolean = False
    Dim mensajeValidacion As String = ""

    Private Sub btnBuscaDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDoc.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "REC"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDDOCUMENTOTextBox.Text = fLDoc.idTipoDoc
                CargaTipoDoc(fLDoc.idTipoDoc)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CargaTipoDoc(ByVal TipoDoc As String)
        Try
            iTipoDoc = New cTipoDocumento(TipoDoc)
            If iTipoDoc Is Nothing Then
                MessageBox.Show("El tipo de documento seleccionado no esta registrado", "Error en tipo de documento", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TipoDoc, iTipoDoc.DocEs)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaTercero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTercero.Click
        Try
            Dim fBT As New fBuscaTercero
            If iTipoDoc.DocEs = 10 Then
                fBT.TipoTercero = "Cliente"
                daCli = New lbGlobalcom.dsTerceroTableAdapters.TER_CLIENTESTableAdapter
            End If
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTERCEROTextBox.Text = fBT.midTercero
                SUCURSALTextBox.Text = fBT.mSucursal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaFormaPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaFormaPago.Click
        Try
            Dim fltp As New fListaTipoPago
            If fltp.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTIPOPAGOTextBox.Text = fltp.idPago
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

   
    Private Sub btnMedioPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMedioPago.Click
        Try
            Dim fListMP As New fListaMediosPago
            If fListMP.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDMEDIOPAGOTextBox.Text = fListMP.IdMedioPag
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsCartera.CAR_RECIBOSCAJA.GetChanges IsNot Nothing Or DsCartera.CAR_RECIBOCAJADETALLEFACTURA.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(True)
                End If
            End If
            CAR_RECIBOSCAJABindingSource.CancelEdit()
            DsCartera.CAR_RECIBOCAJADETALLEFACTURA.Rows.Clear()
            DsCartera.CAR_RECIBOSCAJA.Rows.Clear()
            CAR_RECIBOSCAJABindingSource.AddNew()
            VALORRECIBOTextBox.Text = "0"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar este documento", "Elimina Documento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                CAR_RECIBOSCAJABindingSource.RemoveCurrent()
                lEliminaDoc = True
                SalvaCambios(False)
                MessageBox.Show("Se elimino documento con exito", "Elimino Documento", MessageBoxButtons.OK, MessageBoxIcon.Information)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
   
    Private Sub btnBuscaRecibos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaRecibos.Click
        Try
            Dim flRec As New fListaRecibos
            If flRec.ShowDialog = Windows.Forms.DialogResult.OK Then
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                If DsCartera.CAR_RECIBOSCAJA.GetChanges IsNot Nothing Or DsCartera.CAR_RECIBOCAJADETALLEFACTURA.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        SalvaCambios(False)
                    End If
                End If
                DsCartera.CAR_RECIBOCAJADETALLEPLAN.Rows.Clear()
                DsCartera.CAR_RECIBOSCAJA.Rows.Clear()
                daRecibi.FillByIdDocumento(DsCartera.CAR_RECIBOSCAJA, flRec.pDocumento, flRec.pNumero)
                daRecibiDtalle.FillByIdDocumento(DsCartera.CAR_RECIBOCAJADETALLEPLAN, flRec.pDocumento, flRec.pNumero)
                iTipoDoc = New cTipoDocumento(IDDOCUMENTOTextBox.Text)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CAR_RECIBOCAJADETALLEPLANDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles CAR_RECIBOCAJADETALLEPLANDataGridView.CellBeginEdit
        If NUMEROTextBox.Text <> "" AndAlso IDDOCUMENTOTextBox.Text <> "" Then
            CAR_RECIBOCAJADETALLEPLANDataGridView.Rows(e.RowIndex).Cells("NITEM").Value = cGeneral.GetNumeroLinea(CAR_RECIBOCAJADETALLEPLANDataGridView, e.RowIndex, "NITEM")
        End If
    End Sub

    Private Sub fRecibosPlanes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            daRecibi = New dsCarteraTableAdapters.CAR_RECIBOSCAJATableAdapter
            daRecibiDtalle = New dsCarteraTableAdapters.CAR_RECIBOCAJADETALLEPLANTableAdapter
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub SalvaCambios(ByVal Msj As Boolean)
        Try
            CAR_RECIBOSCAJABindingSource.EndEdit()
            CAR_RECIBOCAJADETALLEPLANBindingSource.EndEdit()
            If Msj Then _
               Validate()
            IDDOCUMENTOTextBox.Enabled = False
            NUMEROTextBox.Enabled = False
            btnBuscaDoc.Enabled = False
            If lEliminaDoc = True Then
                daRecibiDtalle.Update(DsCartera.CAR_RECIBOCAJADETALLEPLAN)
                daRecibi.Update(DsCartera.CAR_RECIBOSCAJA)
                lEliminaDoc = False
            Else
                daRecibi.Update(DsCartera.CAR_RECIBOSCAJA)
                daRecibiDtalle.Update(DsCartera.CAR_RECIBOCAJADETALLEPLAN)
            End If
            If Msj Then _
                MessageBox.Show("Se salvarón cambios con exito", "Salvar Cambios", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub CAR_RECIBOSCAJABindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CAR_RECIBOSCAJABindingNavigatorSaveItem.Click
        Try
            SalvaCambios(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnImprimeRCPlanes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimeRCPlanes.Click
        Try
            Dim DatPlanes As cImprimeRecibo = New cImprimeRecibo(IDDOCUMENTOTextBox.Text, NUMEROTextBox.Text, 2)
            Dim Plan As New rcPlanes
            Plan.PrintOptions.ApplyPageMargins(New CrystalDecisions.Shared.PageMargins(200, 0, 0, 0))
            If My.Settings.TamañoPapelFac = "Carta" Then
                Plan.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLetter
            ElseIf My.Settings.TamañoPapelFac = "Media Carta" Then
                Plan.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperStatement
                Plan.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape
            End If
            Plan.SetDataSource(DatPlanes.DatosRecibo)
            Dim Para As cParametrizacion = New cParametrizacion("NOMEMPRE", "EMPRESA")
            Plan.SetParameterValue("PEMPRESA", Para.Valor.ToString)
            Para = New cParametrizacion("NIT", "EMPRESA")
            Plan.SetParameterValue("PNIT", Para.Valor.ToString)
            Dim iSede As New cSedes(CustomPrincipal.IDSede)
            Plan.SetParameterValue("PSUCURSAL", iSede.nombre & vbNewLine & iSede.direccion & vbNewLine & iSede.Ciudad)
            Dim fPPvRCPlanes As New fImprimeRCPlanes
            fPPvRCPlanes.Reporte = Plan
            fPPvRCPlanes.Show()
            'Plan.PrintToPrinter(1, True, 0, 0)
            Plan = Nothing
            DatPlanes = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class